#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

int main() {
    const char* fifoPath = "/tmp/my_fifo";

    // 创建命名管道
    mkfifo(fifoPath, 0666);

    char buffer[100];
    
    // 打开管道以进行读取
    int fd = open(fifoPath, O_RDONLY);
    if (fd < 0) {
        perror("Failed to open FIFO");
        return 1;
    }

    std::cout << "Server: Waiting for data..." << std::endl;

    // 从管道中读取数据
    ssize_t bytesRead = read(fd, buffer, sizeof(buffer) - 1);
    if (bytesRead > 0) {
        buffer[bytesRead] = '\0'; // 添加字符串终止符
        std::cout << "Server received: " << buffer << std::endl;
    }

    // 关闭管道
    close(fd);
    unlink(fifoPath); // 删除管道

    return 0;
}